<?php 

namespace app\admin\event;

use app\common\model\ShopGroup as ShopGroupModel;
use app\common\utils\JsonService;

class ShopGroupEvent{

    /*
    * 店铺分组列表
    */
    public static function shopGroupList(){
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;
        $where = [];
        if($id = isset($params['id'])){
                    $where[] = [['id','=',$params['id']]];
            }
            if($group_name = isset($params['group_name'])){
                    $where[] = [['group_name','like',"%{$params['group_name']}%"]];
            }
            if($sort_order = isset($params['sort_order'])){
                    $where[] = [['sort_order','like',"%{$params['sort_order']}%"]];
            }
             $condition = [
            'where' => $where,
            'order' => ['id' => 'desc'],
                'paginate' => [
                'page' => $page,
                'query' => $params,
            ],
        ];
        $shopGroup = ShopGroupModel::getPageAll($condition);
        return $shopGroup;
    }


    /*
    * 新增店铺分组
    */
    public static function createShopGroup($data){

        try {
            ShopGroupModel::create($data);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }

    /*
    * 更新店铺分组
    */
    public static function updateShopGroup($data){

        try {
            ShopGroupModel::updateOne($data, ['id' => $data['id']]);
        } catch (\Exception $ex) {
            return JsonService::error($ex->getMessage());
        }
        return JsonService::success();
    }


}

